package com.flash.variety.service.impl;

import com.aliyun.oss.OSS;
import com.flash.variety.domain.TbCuisine;
import com.flash.variety.utils.PoiUtils;
import com.ruoyi.common.core.utils.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @description: TODO
* @author admin
* @date 2023/1/815:02
* @version 1.0
*/
@Component
public class MyAsync {

    @Value("${oss.bucket}")
    private String bucketName;

    @Value("${oss.hostName}")
    private String hostName;

    @Autowired
    private OSS client;

    @Autowired
    RedisTemplate redisTemplate;

    @Async("taskExecutor")  //使用taskExecutor线程池
    public String sms(List<TbCuisine> list,Long userId) throws IOException {
        Map map = PoiUtils.poi(list);
        String path = (String) map.get("path");
        String fileName = (String) map.get("fileName");
        InputStream inputStream = new FileInputStream(path);
        try {
            client.putObject(bucketName,fileName,inputStream);
        } catch (Exception e) {

        }
        System.out.println(hostName+fileName);
        File file =new File(path);
        file.delete();
        redisTemplate.opsForValue().set("url"+userId, hostName+fileName);
        return   hostName+fileName;
    }


    @Autowired
    TbCuisineServiceImpl tbCuisineService;

    @Async("taskExecutor")
    public void dr(MultipartFile file) throws IOException {
        System.out.println("a"+Thread.currentThread().getName());
        List<TbCuisine> studentList = new ArrayList<>();
        String fileName = file.getOriginalFilename();
        String name = fileName.substring(0, fileName.indexOf("."));
        String hzm =fileName.substring(name.length()+1);
        if (StringUtils.equals(hzm,"xlsx")) {
            //根据路径获取这个操作excel表格
            XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
            //根据页面index 获取sheet页
            XSSFSheet sheet = wb.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1; i <= lastRowNum; i++) {
                XSSFRow row = sheet.getRow(i);
                if (row!= null) {
                    List<String> list = new ArrayList<>();
                    for (Cell cell : row) {
                        if (cell!=null){
                            cell.setCellType(CellType.STRING);
                            String value = cell.getStringCellValue();
                            if (value!=null && !"".equals(value)){
                                list.add(value);
                            }

                        }
                    }
                    if (list.size()>0){

                        TbCuisine tbCuisine = new TbCuisine();
                        String stringCellValue = row.getCell(1).getStringCellValue();
                        int id = (int)Double.parseDouble(stringCellValue);
                        tbCuisine.setCuisineId(id);
                        tbCuisine.setCuisineName(row.getCell(2).getStringCellValue());
                        BigDecimal bigDecimal = new BigDecimal(row.getCell(4).getStringCellValue());
                        tbCuisine.setCuisineMoney(bigDecimal);
                        tbCuisine.setCuisineDescribe(row.getCell(3).getStringCellValue());
                        tbCuisine.setCuisineImage(row.getCell(5).getStringCellValue());
                        String stringCellValue1 = row.getCell(6).getStringCellValue();
                        String stringCellValue2 = row.getCell(7).getStringCellValue();
                        int type = (int) Double.parseDouble(stringCellValue1);
                        tbCuisine.setCuisineType(type);
                        int i1 = (int) Double.parseDouble(stringCellValue2);
                        Long  commercialId= new Long((long)i1);
                        tbCuisine.setCommercialId(i1);
                        studentList.add(tbCuisine);
//                    System.out.println(studentList);
                    }
                }
            }
        } else if (StringUtils.equals(hzm,"xls")) {
            //根据路径获取这个操作excel表格
            HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
            //根据页面index 获取sheet页
            HSSFSheet sheet = wb.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1; i <= lastRowNum; i++) {
                HSSFRow row = sheet.getRow(i);
                if (row!= null) {
                    List<String> list = new ArrayList<>();
                    for (Cell cell : row) {
                        if (cell!=null){
                            cell.setCellType(CellType.STRING);
                            String value = cell.getStringCellValue();
                            if (value!=null && !"".equals(value)){
                                list.add(value);
                            }

                        }
                    }
                    if (list.size()>0){
                        TbCuisine tbCuisine = new TbCuisine();
                        String stringCellValue = row.getCell(1).getStringCellValue();
                        int id = (int)Double.parseDouble(stringCellValue);
                        tbCuisine.setCuisineId(id);
                        tbCuisine.setCuisineName(row.getCell(2).getStringCellValue());
                        BigDecimal bigDecimal = new BigDecimal(row.getCell(4).getStringCellValue());
                        tbCuisine.setCuisineMoney(bigDecimal);
                        tbCuisine.setCuisineDescribe(row.getCell(3).getStringCellValue());
                        tbCuisine.setCuisineImage(row.getCell(5).getStringCellValue());
                        String stringCellValue1 = row.getCell(6).getStringCellValue();
                        String stringCellValue2 = row.getCell(7).getStringCellValue();
                        int type = (int) Double.parseDouble(stringCellValue1);
                        tbCuisine.setCuisineType(type);
                        int i1 = (int) Double.parseDouble(stringCellValue2);
                        Long  commercialId= new Long((long)i1);
                        tbCuisine.setCommercialId(i1);
                        studentList.add(tbCuisine);
//                    System.out.println(studentList);
                    }
                }
            }
        }
        studentList.forEach(tbCuisine -> {
            //添加菜品
            tbCuisineService.insertTbCuisine(tbCuisine);
        });
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(format.format(date));
    }
}
